package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Farmer;
import com.ruoyi.system.service.IFarmerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参合农民档案管理Controller
 *
 * @author moweiyu
 * @date 2021-06-09
 */
@Controller
@RequestMapping("/system/farmer")
public class FarmerController extends BaseController
{
    private String prefix = "system/farmer";

    @Autowired
    private IFarmerService farmerService;

    @RequiresPermissions("system:farmer:view")
    @GetMapping()
    public String farmer()
    {
        return prefix + "/farmer";
    }

    /**
     * 查询参合农民档案管理列表
     */
    @RequiresPermissions("system:farmer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Farmer farmer)
    {
        startPage();
        List<Farmer> list = farmerService.selectFarmerList(farmer);
        return getDataTable(list);
    }

    /**
     * 导出参合农民档案管理列表
     */
    @RequiresPermissions("system:farmer:export")
    @Log(title = "参合农民档案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Farmer farmer)
    {
        List<Farmer> list = farmerService.selectFarmerList(farmer);
        ExcelUtil<Farmer> util = new ExcelUtil<Farmer>(Farmer.class);
        return util.exportExcel(list, "参合农民档案管理数据");
    }

    /**
     * 新增参合农民档案管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存参合农民档案管理
     */
    @RequiresPermissions("system:farmer:add")
    @Log(title = "参合农民档案管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Farmer farmer)
    {
        return toAjax(farmerService.insertFarmer(farmer));
    }

    /**
     * 修改参合农民档案管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Farmer farmer = farmerService.selectFarmerById(id);
        mmap.put("farmer", farmer);
        return prefix + "/edit";
    }

    /**
     * 修改保存参合农民档案管理
     */
    @RequiresPermissions("system:farmer:edit")
    @Log(title = "参合农民档案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Farmer farmer)
    {
        return toAjax(farmerService.updateFarmer(farmer));
    }

    /**
     * 删除参合农民档案管理
     */
    @RequiresPermissions("system:farmer:remove")
    @Log(title = "参合农民档案管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(farmerService.deleteFarmerByIds(ids));
    }
}
